package com.wf.course.web.controller.comm;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.utils.page.PageBean;
import com.wf.course.model.comm.Question;
import com.wf.course.service.IQuestionService;
import com.wf.course.web.Interceptor.LogInfo;

/**
 * 常见问题
 * @author TT
 *
 */
@RequestMapping("question")
@Controller
public class QuestionController {
	
	@Autowired
	private IQuestionService questionService;
	
	@LogInfo("查询常看问题列表")
	@RequestMapping("questionList")
	public String questionList(@RequestParam(value = "pageNo",required = false) Integer pageNo,
							   @RequestParam(value = "pageSize",required = false) Integer pageSize,
							   @RequestParam(value = "title",required = false) String title,
							   Model model){
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("title", title);
		
		PageBean<Question> pageBean = new PageBean<Question>(pageNo,pageSize,null,map);
		
		questionService.getQuestionList(pageBean);
		pageBean.setUrl("/question/questionList");
		model.addAttribute("page",pageBean);
		
		return "comm/questionList";
	}
	
	/**
	 * 新增修改页面信息加载
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("新增/修改常见问题，页面数据初始化")
	@RequestMapping("questionLoad/{id}")
	public String questionLoad(@PathVariable("id") Integer id,Model model){
		if (id != null && id != 0) {
			Question question = this .questionService.questionLoad(id);
			model.addAttribute("question",question);
			model.addAttribute("type","upd");
		}else {
			model.addAttribute("type","add");
		}
		
		return "/comm/questionEdit";
	}
	
	/**
	 * 添加或修改常见问题
	 * @param question
	 * @param request
	 * @return
	 */
	@LogInfo("新增/修改常见问题")
	@RequestMapping("questionOption")
	public String questionOption(@ModelAttribute Question question,
			HttpServletRequest request){
		//添加常见问题
		if (question.getId() == null) {
			//保存
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			question.setUpdate_time(simpleDateFormat.format(new Date()));
			questionService.saveQuestion(question);
		}else{
			questionService.updateQuesiton(question);
		}
		
		return "redirect:/question/questionList";
	}
	
	/**
	 * 删除常见问题
	 * @param id
	 * @return
	 */
	@LogInfo("删除常见问题")
	@RequestMapping("deleteQuestion/{id}")
	public String deleteQuestion(@PathVariable Integer id){
		this.questionService.deleteQuestion(id);
		return "redirect:/question/questionList";
	}
}
